package com.talkyun.openx;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * OpenX Header Manager
 * <p/>
 * Created by kevin on 3/23 2016.
 */
public class RestfulHeaderManager {
    private static final RestfulHeaderManager instance = new RestfulHeaderManager();
    private static final Map<String, String> GLOBAL_HEAD = new ConcurrentHashMap<String, String>();
    private static final ThreadLocal<Map<String, String>> HEAD_LOCAL = new ThreadLocal<Map<String, String>>() {
        @Override
        protected Map<String, String> initialValue() {
            return new HashMap<String, String>();
        }
    };

    private RestfulHeaderManager() {

    }

    public static RestfulHeaderManager instance() {
        return instance;
    }

    public static RestfulHeaderManager get() {
        return instance;
    }

    public void addHead(String key, String val) {
        GLOBAL_HEAD.put(key, val);
    }

    public void addOnceHead(String key, String val) {
        getOnceHead().put(key, val);
    }

    public Map<String, String> getHead() {
        Map<String, String> head = HEAD_LOCAL.get();
        return head != null ? head : GLOBAL_HEAD;
    }

    public Map<String, String> getOnceHead() {
        return HEAD_LOCAL.get();
    }
}